package com.sheyu.mall.product.app.attr;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.sheyu.mall.product.entity.attr.AttrEntity;
import com.sheyu.mall.product.service.attr.AttrAttrgroupRelationService;
import com.sheyu.mall.product.service.attr.AttrService;
import com.sheyu.mall.product.service.category.CategoryService;
import com.sheyu.mall.product.service.attr.vo.AttrGroupRelationVo;
import com.sheyu.mall.product.service.attr.vo.AttrGroupwithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sheyu.mall.product.entity.attr.AttrGroupEntity;
import com.sheyu.mall.product.service.attr.AttrGroupService;
import com.sheyu.common.utils.PageUtils;
import com.sheyu.common.utils.R;



/**
 * 属性分组
 *
 * @author sheyu
 * @email sheyu@XXX.com
 * @date 2020-07-27 13:43:00
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteAttrgroupRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        relationService.deleteRealtion(attrGroupRelationVo);
        return R.ok();
    }
    /**
     * 获取属性分组有关联的其他属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrgroupRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntityList = attrGroupService.queryAttrByAttrgroupId(attrgroupId);
        return R.ok().put("data",attrEntityList);
    }

    @PostMapping("/attr/relation")
    public R attrgroupRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){
//       relationService.queryRelationByAttrId(attrGroupRelationVos);
       relationService.saveAttrGroupRelations(attrGroupRelationVos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组及关联属性
     *
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupwithAttrsVo> attrGroupwithAttrsVos =  attrGroupService.getAttrGroupAttrs(catelogId);
        return R.ok().put("data",attrGroupwithAttrsVos);
    }
    /**
     * 获取属性分组没有关联的其他属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrgroupNoRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.queryAttrByNoRelationAttrgroupId(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /**
     * 根据catId得到结果
     */
    @GetMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R listByCatId(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPageByID(params,catelogId);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();
		Long[] paths = categoryService.categoryPath(catelogId);
        attrGroup.setCatelogPath(paths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroupEntity){
		attrGroupService.save(attrGroupEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
        AttrGroupEntity attrGroupServiceById = attrGroupService.getById(attrGroup.getAttrGroupId());
        if (attrGroupServiceById!=null){
            attrGroupService.updateById(attrGroup);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
